package com.shaobufan.museum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaobufan.museum.mapper.UserScheduleMapper;
import com.shaobufan.museum.pojo.entity.UserSchedule;
import com.shaobufan.museum.pojo.vo.UserScheduleVo;
import com.shaobufan.museum.service.UserScheduleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 17126
 * &#064;description  针对表【user_schedule(中间表)】的数据库操作Service实现
 * &#064;createDate  2024-03-11 21:31:57
 */
@Service
public class UserScheduleServiceImpl extends ServiceImpl<UserScheduleMapper, UserSchedule>
        implements UserScheduleService {
    @Resource
    UserScheduleMapper userScheduleMapper;

    /**
     * 根据用户id获取用户日程表
     *
     * @param userId 用户id
     * @return 用户日程实体
     */
    @Override
    public List<UserScheduleVo> scheduleSearch(Integer userId) {
        QueryWrapper<UserSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);

        List<UserSchedule> userSchedulesList;
        userSchedulesList = userScheduleMapper.selectList(queryWrapper);
        List<UserScheduleVo> userScheduleVoList;
        userScheduleVoList = userSchedulesList.stream().map(this::getUserScheduleVo).collect(Collectors.toList());
        return userScheduleVoList;
    }

    /**
     * 插入用户日程表
     *
     * @param userId     用户id
     * @param scheduleId 日程id
     * @return 日程id
     */
    @Override
    public Integer insertSchedule(Integer userId, Integer scheduleId) {
        UserSchedule userSchedule = new UserSchedule();
        userSchedule.setUserId(userId);
        userSchedule.setScheduleId(scheduleId);
        int insert = userScheduleMapper.insert(userSchedule);
        if (insert != 1) {
            return null;
        }
        return userSchedule.getScheduleId();
    }

    /**
     * 删除用户日程表
     *
     * @param userId 用户id
     * @param scheduleId 日程id
     * @return 删除日程条目数
     */
    @Override
    public Integer deleteSchedule(Integer userId, Integer scheduleId){
        QueryWrapper<UserSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("schedule_id",scheduleId);
        int deleted;
        deleted = userScheduleMapper.delete(queryWrapper);
        return deleted;
    }

    /**
     * 获取 UserScheduleVo
     *
     * @param userSchedule 传入的实体类
     * @return UserScheduleVo
     */
    @Override
    public UserScheduleVo getUserScheduleVo(UserSchedule userSchedule) {
        UserScheduleVo userScheduleVo = new UserScheduleVo();
        userScheduleVo.setUserId(userSchedule.getUserId());
        userScheduleVo.setScheduleId(userSchedule.getScheduleId());
        return userScheduleVo;
    }
}